import itertools

from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from common.database import get_db
from core.response import resp_400, resp_200
from app.fault_self_recovery.handlers.fsr_container_hpa_handler import FSRContainerHpaHandler
from app.fault_self_recovery.handlers.fsr_host_disk_handler import FSRHostDiskHandler
from app.fault_self_recovery.handlers.fsr_rabbitmq_queue_handler import FSRRabbitmqQueueHpaHandler
from core.security import permission_checker
from app.fault_self_recovery.schemas import *
from core.logger import logger


router = APIRouter()

#### HPA服务配置接口 ####
@router.get('/container/hpa/', summary="获取动态扩缩容服务信息, 根据表字段-模糊匹配", description="故障自愈-动态扩缩容")
async def get_fsr_container_hpa_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1),
                                 per_page: int = Query(10, ge=0),
                                 db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_container_hpa_handler = FSRContainerHpaHandler()
    status, result = fsr_container_hpa_handler.get_container_hpa_search(db, page, per_page, field, value)
    if not status:
        message = "get container hpa search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/container/hpa/match/', summary="获取动态扩缩容服务信息，根据表字段-完全匹配", description="故障自愈-动态扩缩容")
async def get_fsr_container_hpa_match(field: str = Query(..., description="需要查询的字段"),
                                value: str = Query(..., description="字段的值"),
                                page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                                db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_container_hpa_handler = FSRContainerHpaHandler()
    result = fsr_container_hpa_handler.get_container_hpa_match(db, page, per_page, field, value)
    if not result:
        message = "get container hpa match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)



@router.post('/container/hpa/', summary="增加动态扩缩容服务信息", description="故障自愈-动态扩缩容")
async def post_fsr_container_hpa(data: PostContainerHpa, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_container_hpa_handler = FSRContainerHpaHandler()
    result = fsr_container_hpa_handler.add_container_hpa(db, data.dict())
    if not result:
        message = "post container hpa failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.put('/container/hpa/', summary="修改动态扩缩容服务信息", description="故障自愈-动态扩缩容")
async def put_fsr_container_hpa(data: PutContainerHpa, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_container_hpa_handler = FSRContainerHpaHandler()
    result = fsr_container_hpa_handler.update_container_hpa(db, data.dict())
    if not result:
        message = "put container hpa failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.delete('/container/hpa/', summary="删除动态扩缩容服务信息", description="故障自愈-动态扩缩容")
async def delete_fsr_container_hpa(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_container_hpa_handler = FSRContainerHpaHandler()
    result = fsr_container_hpa_handler.delete_container_hpa(db, data.dict())
    if not result:
        message = "delete container hpa failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()








#### 服务器磁盘自愈配置接口 ####
@router.get('/host/disk/', summary="获取服务器磁盘自愈配置信息, 根据表字段-模糊匹配", description="故障自愈-磁盘清理")
async def get_frs_host_disk_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1),
                                 per_page: int = Query(10, ge=0),
                                 db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_host_disk_handler = FSRHostDiskHandler()
    status, result = fsr_host_disk_handler.get_host_disk_search(db, page, per_page, field, value)
    if not status:
        message = "get host disk search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/host/disk/match/', summary="获取服务器磁盘自愈配置信息, 根据表字段-完全匹配", description="故障自愈-磁盘清理")
async def get_frs_host_disk_match(field: str = Query(..., description="需要查询的字段"),
                                value: str = Query(..., description="字段的值"),
                                page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                                db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_host_disk_handler = FSRHostDiskHandler()
    result = fsr_host_disk_handler.get_host_disk_match(db, page, per_page, field, value)
    if not result:
        message = "get host disk match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)



@router.post('/host/disk/', summary="增加服务器磁盘自愈配置信息", description="故障自愈-磁盘清理")
async def post_frs_host_disk(data: PostHostDisk, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_host_disk_handler = FSRHostDiskHandler()
    result = fsr_host_disk_handler.add_host_disk(db, data.dict())
    if not result:
        message = "post host disk failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.put('/host/disk/', summary="修改服务器磁盘自愈配置信息", description="故障自愈-磁盘清理")
async def put_frs_host_disk(data: PutHostDisk, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_host_disk_handler = FSRHostDiskHandler()
    result = fsr_host_disk_handler.update_host_disk(db, data.dict())
    if not result:
        message = "put host disk failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.delete('/host/disk/', summary="删除服务器磁盘自愈配置信息", description="故障自愈-磁盘清理")
async def delete_frs_host_disk(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_host_disk_handler = FSRHostDiskHandler()
    result = fsr_host_disk_handler.delete_host_disk(db, data.dict())
    if not result:
        message = "delete host disk failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()







#### rabbitmq队列配置接口 ####
@router.get('/rabbitmq/queue/', summary="获取rabbitmq队列配置信息, 根据表字段-模糊匹配", description="故障自愈-动态扩缩容")
async def get_fsr_rabbitmq_queue_search(field: str = Query("all"), value: str = Query(""), page: int = Query(1, ge=1),
                                 per_page: int = Query(10, ge=0),
                                 db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_rabbitmq_queue_handler = FSRRabbitmqQueueHpaHandler()
    status, result = fsr_rabbitmq_queue_handler.get_rabbitmq_queue_search(db, page, per_page, field, value)
    if not status:
        message = "get rabbitmq queue search failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)


@router.get('/rabbitmq/queue/match/', summary="获取rabbitmq队列配置信息, 根据表字段-完全匹配", description="故障自愈-动态扩缩容")
async def get_fsr_rabbitmq_queue_match(field: str = Query(..., description="需要查询的字段"),
                                value: str = Query(..., description="字段的值"),
                                page: int = Query(1, ge=1), per_page: int = Query(10, ge=0),
                                db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_rabbitmq_queue_handler = FSRRabbitmqQueueHpaHandler()
    result = fsr_rabbitmq_queue_handler.get_rabbitmq_queue_match(db, page, per_page, field, value)
    if not result:
        message = "get rabbitmq queue match failed"
        logger.error(f"{message} => page:{page} per_page:{per_page} field:{field} value:{value}")
        return resp_400(message)
    return resp_200(result)



@router.post('/rabbitmq/queue/', summary="增加rabbitmq队列配置信息", description="故障自愈-动态扩缩容")
async def post_fsr_rabbitmq_queue(data: PostRabbitmqQueue, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_rabbitmq_queue_handler = FSRRabbitmqQueueHpaHandler()
    result = fsr_rabbitmq_queue_handler.add_rabbitmq_queue(db, data.dict())
    if not result:
        message = "post rabbitmq queue failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.put('/rabbitmq/queue/', summary="修改rabbitmq队列配置信息", description="故障自愈-动态扩缩容")
async def put_fsr_rabbitmq_queue(data: PutRabbitmqQueue, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_rabbitmq_queue_handler = FSRRabbitmqQueueHpaHandler()
    result = fsr_rabbitmq_queue_handler.update_rabbitmq_queue(db, data.dict())
    if not result:
        message = "put rabbitmq queue failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()


@router.delete('/rabbitmq/queue/', summary="删除rabbitmq队列配置信息", description="故障自愈-动态扩缩容")
async def delete_fsr_rabbitmq_queue(data: DelBaseModel, db: Session = Depends(get_db), user=Depends(permission_checker)):
    fsr_rabbitmq_queue_handler = FSRRabbitmqQueueHpaHandler()
    result = fsr_rabbitmq_queue_handler.delete_rabbitmq_queue(db, data.dict())
    if not result:
        message = "delete rabbitmq queue failed"
        logger.error(f"{message} => data:{data.dict()}")
        return resp_400(message)
    return resp_200()

